import { Body, Controller, Get, Ip, Post, Req } from '@nestjs/common';
import { LoginService } from './login.service';
import { ResponseUtilService } from '@app/response-util';
import { DtoPipe } from '../../pipe/dto/dto.pipe';
import RegisterDto from './dto/register.dto';
import { ReqAttrs } from '../../constant';
import LoginSmsDto from './dto/login.sms.dto';
import LoginDto from './dto/login';
import ResetPasswordDto from './dto/resetPassword.dto';





@Controller('login')
export class LoginController {
    constructor(
        private service:LoginService,
    ){}

    //发送注册验证码
    @Post('send/regiser/sms')
    async sendRegisterSms(@Body('phone') phone:string) {
        try {

            let expire = await this.service.getRegisterSmsResendExpire(phone)
            if(expire > 5) {
                return ResponseUtilService.successRes(expire)
            }
            expire =  await this.service.sendRegisterSms(phone)
            return expire == null ? ResponseUtilService.failRes() : ResponseUtilService.successRes(expire)
        } catch (e) {
            return ResponseUtilService.failRes(e.message)
        }
        

    }
    //注册
    @Post('register')
    async register(@Body(new DtoPipe()) dto:RegisterDto, @Req() req:ReqAttrs, @Ip() ip:string) {
        try {
            const r =  await this.service.register(dto.phone, dto.code, dto.name)
            return r ? ResponseUtilService.successRes(r) : ResponseUtilService.failRes()
        } catch (error) {
            return ResponseUtilService.failRes(error.message)
        }
    }


    //发送登录验证码
    @Post('send/login/sms')
    async sendLoginSms(@Body('phone') phone:string) {
        try {

            let expire = await this.service.getLogoinSmsResendExpire(phone)
            if(expire > 5) {
                return ResponseUtilService.successRes(expire)
            }
            expire =  await this.service.sendLoginSms(phone)
            return expire == null ? ResponseUtilService.failRes() : ResponseUtilService.successRes(expire)
        } catch (e) {
            return ResponseUtilService.failRes(e.message)
        }
        

    }
    //短信验证码登录
    @Post('login/sms')
    async loginSms(@Body(new DtoPipe()) dto:LoginSmsDto) {
        try {
            const r =  await this.service.loginBySms(dto.phone, dto.code)
            return r ? ResponseUtilService.successRes(r) : ResponseUtilService.failRes()
        } catch (error) {
            return ResponseUtilService.failRes(error.message)
        }
    }

    //密码登录
    @Post('login')
    async login(@Body(new DtoPipe()) dto:LoginDto) {
        try {
            const r =  await this.service.login(dto.phone, dto.password)
            return r ? ResponseUtilService.successRes(r) : ResponseUtilService.failRes()
        } catch (error) {
            return ResponseUtilService.failRes(error.message)
        }
    }



    //发送忘记密码验证码
    @Post('send/forget/sms')
    async sendForgetSms(@Body('phone') phone:string) {
        try {

            let expire = await this.service.getforgetResendExpire(phone)
            if(expire > 5) {
                return ResponseUtilService.successRes(expire)
            }
            expire =  await this.service.sendForgetSms(phone)
            return expire == null ? ResponseUtilService.failRes() : ResponseUtilService.successRes(expire)
        } catch (e) {
            return ResponseUtilService.failRes(e.message)
        }
    }

    @Post('forget/check')
    async forgetcheck(@Body(new DtoPipe()) dto:RegisterDto, @Req() req:ReqAttrs, @Ip() ip:string) {
        try {
            const r =  await this.service.forgetcheck(dto.phone, dto.code)
            return r ? ResponseUtilService.successRes(r) : ResponseUtilService.failRes()
        } catch (error) {
            return ResponseUtilService.failRes(error.message)
        }
    }

    @Post('reset/password')
    async resetPassword(@Body(new DtoPipe()) dto:ResetPasswordDto, @Req() req:ReqAttrs, @Ip() ip:string) {
        try {
            const r =  await this.service.resetPassword(dto.phone, dto.nonce, dto.password)
            return r ? ResponseUtilService.successRes(r) : ResponseUtilService.failRes()
        } catch (error) {
            return ResponseUtilService.failRes(error.message)
        }
    }


    @Post('refresh')
    public async refresh(@Req() req) {
        try {
            const r = await this.service.refresh(req[ReqAttrs.USER_ID])

            return ResponseUtilService.successRes(r)

        } catch (error) {
            
            return ResponseUtilService.failRes(error.message)
        }
    }
}
